package com.gitee.vueantd.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

@Slf4j
public class PubUtil {


  public static Long getLong(String v, Long defaultVal) {
    try {
      if (StringUtils.isNotEmpty(v)) {
        return Long.parseLong(v);
      }
    } catch (Exception e) {
      log.error("{}", e);
    }
    return defaultVal;
  }

  public static Integer getInteger(String v, Integer defaultVal) {
    try {
      if (StringUtils.isNotEmpty(v)) {
        return Integer.parseInt(v);
      }
    } catch (Exception e) {
      log.error("{}", e);
    }
    return defaultVal;
  }

  public static void sleepMS(long ms) {
    try {
      Thread.sleep(ms);
    } catch (InterruptedException e) {
    }
  }

  public static void sleepS(long second) {
    sleepMS(second * 1000);
  }

  public static String paddingId(Long id) {
    return StringUtils.leftPad("" + id, 8, "0");
  }

  public static boolean isFile(File file) {
    return (file != null && file.exists() && file.isFile());
  }

  public static boolean isFile(String fileName) {
    if (StringUtils.isNotEmpty(fileName)) {
      return isFile(FileUtils.getFile(fileName));
    }
    return false;
  }

  public static void deleteFile(File file) {
    if (PubUtil.isFile(file)) {
      FileUtils.deleteQuietly(file);
    }
  }
}
